package sk.velkavrana.app.databaseInfo.dao.xml;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import sk.velkavrana.app.databaseInfo.dao.ConnectionInfoDao;
import sk.velkavrana.app.databaseInfo.dao.DatabaseInfoDao;
import sk.velkavrana.app.databaseInfo.model.ConnectionInfo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author mtymes
 * @since Sep 22, 2009 2:46:49 AM
 */
public class ConnectionInfoDaoXml extends AbstractDaoXml<ConnectionInfo, String> implements ConnectionInfoDao {

    private DatabaseInfoDao databaseInfoDao;


    protected ConnectionInfo getFromXml(String fileName) {
        ConnectionInfo connectionInfo = null;

        SAXReader reader = new SAXReader();
        try {
            File file = new File(fileName);

            if (file.exists()) {
                Document document = reader.read(file);

                Element root = document.getRootElement();

                if ("connection".equals(root.getName())) {
                    connectionInfo = new ConnectionInfo();

                    connectionInfo.setId(file.getName().split("\\.")[0]);
                    connectionInfo.setUrl(root.attribute("url").getValue());
                    connectionInfo.setLogin(root.attribute("login").getValue());
                    connectionInfo.setPassword(root.attribute("password").getValue());
                    connectionInfo.setSchema(root.attribute("schema").getValue());

                    Attribute databaseInfoIdAttr = root.attribute("databaseInfoId");
                    if (databaseInfoIdAttr != null) {
                        connectionInfo.setDatabaseInfo(databaseInfoDao.getById(databaseInfoIdAttr.getValue()));
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            connectionInfo = null;
        }

        return connectionInfo;
    }


    public void save(ConnectionInfo bean) {
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("connection");

        root.addAttribute("url", bean.getUrl());
        root.addAttribute("login", bean.getLogin());
        root.addAttribute("password", bean.getPassword());
        root.addAttribute("schema", bean.getSchema());
        if (bean.getDatabaseInfo() != null) {
            root.addAttribute("databaseInfoId", bean.getDatabaseInfo().getId());
        }

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer =
                    new XMLWriter(
                            new FileWriter(getFullFileName(bean.getId())),
                            format
                    );
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public DatabaseInfoDao getDatabaseInfoDao() {
        return databaseInfoDao;
    }


    public void setDatabaseInfoDao(DatabaseInfoDao databaseInfoDao) {
        this.databaseInfoDao = databaseInfoDao;
    }
}
